package com.hotel.homehotel.before.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hotel.homehotel.before.entiy.Room;
import com.hotel.homehotel.before.entiy.User;
import com.hotel.homehotel.before.entiy.UserHobby;
import com.hotel.homehotel.before.mapper.RoomMapper;
import com.hotel.homehotel.before.mapper.UserHobbyMapper;
import com.hotel.homehotel.before.mapper.UserMapper;
import com.hotel.homehotel.before.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class RoomServiceImpl  implements RoomService {
     @Autowired
    RoomMapper roomMapper;
     @Autowired
    UserHobbyMapper userHobbyMapper;
     @Autowired
    UserMapper userMapper;
    public List<Room> getAllRoom(String uid){
        System.out.println("uiduiduiduid"+uid);
        QueryWrapper<Room> queryWrapper=new QueryWrapper();
        if("-1".equals(uid)) {
            queryWrapper.orderByDesc("room_count");
            return  roomMapper.selectList(queryWrapper);
        }
        else{
            User user = userMapper.selectById(uid);
            String hobby = user.getHobby();
                  queryWrapper.eq("room_category",hobby);
            return  roomMapper.selectList(queryWrapper);
        }

    }

    public Room getRoomById(String id){
        QueryWrapper<Room> queryWrapper=new QueryWrapper();

        Room room = roomMapper.selectById(id);
        room.setCount(room.getCount()+50);
        roomMapper.updateById(room);
        return room;

    }

    public boolean insertRoom(Room room){
        UserHobby userHobby=new UserHobby();

        String s = UUID.randomUUID().toString().replace("-", "").substring(0,10);
        room.setId(s);
        System.out.println(s);
        roomMapper.insert(room);
        userHobby.setRid(s);
        userHobby.setNum(0);
        userHobby.setUid("admin");
       userHobbyMapper.insert(userHobby);
        return true;
    }

    public boolean updateRoom(Room room){
        return  roomMapper.updateById(room)>0;
    }
    public boolean deleteRoom(String id){
        return roomMapper.deleteById(id)>0;
    }
    public List<Room> getRoomByIByRoomName(String name,String uid){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.like("room_name",name);
        return  roomMapper.selectList(queryWrapper);
    }



}
